﻿using System;
using System.IO;
using System.Net;
using Newtonsoft.Json;

namespace Softelligent.Zintra.Demos.Events.PublishConsole
{
    // TODO create in WPF
    class Program
    {
        private const string ContentType = "text/xml; charset=utf-8";
        //private const string ContentType = "application/json; charset=utf-8";
        //private const string ContentType = "text/plain; charset=utf-8";

        private const string EventsUri = "https://localhost/C/Events/";
        private const string SubscriptionId = "0C27FBFE-9828-4D0E-936A-9EE200DBB949";
        private const string ProductId = "de582afd-5247-4bfd-af4d-9eee00cbeb50";
        private const string HttpMethod = "POST";
        private const string EventName = "TelephoneCall";
         
        static void Main(string[] args)
        {
            string url = string.Concat(EventsUri, ProductId, "/", EventName, "/", SubscriptionId);

            var jsonMessage = JsonConvert.SerializeObject(new TelephoneCall
            {
                FromNumber = "+32132121212",
                ToNumber = "+32132111111",
                Time = DateTime.UtcNow
            });

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = HttpMethod;
            request.ContentType = ContentType;
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write("<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + jsonMessage + "</string>");
                streamWriter.Flush();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Console.WriteLine("STATUS {0}", response.StatusCode);
            }

            Console.WriteLine("JSON written to Zintra Events");
            Console.ReadLine();
        }
    }
}
